package com.lw.config;

import com.lw.interceptor.MiniInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Value("${file.base.path:/temp/user/images/}")
    private String fileBasePath = "/temp/files";//文件上传路径

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*
        将 /temp/user/files/ 的本地资源映射成网络资源，路径是/images/XXX.png
        classpath:/META-INF/resources/ 是配置的swagger路径，swagger是将静态资源放在此路径，
        如果不增加这个classpath下的路径，则swagger无法访问。
         */
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/") //swagger的静态资源
                .addResourceLocations("file:"+fileBasePath);

    }

    /**
     * 相当于以前在xml中<bean class=""></bean>配置
     * @return
     */
    @Bean
    public MiniInterceptor miniInterceptor(){
        return new MiniInterceptor();
    }

    @Bean(initMethod = "init")
    public ZKCuratorClient zkCuratorClient(){
        ZKCuratorClient zkCuratorClient = new ZKCuratorClient();
        return zkCuratorClient;
    }

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(miniInterceptor())
                .addPathPatterns("/user/**","/video/**")//配置要拦截的url
                .excludePathPatterns("/regist/**")//不拦截的url
        ;
        super.addInterceptors(registry);
    }
}
